﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Magnet.Engine;
using Magnet;
namespace Magnet.Engine.Entities
{
    public class Player : Magnet
    {
        public enum PlayerState
        {
            SlowMotion, Spawning, Normal, Dying, Moving ,RotateLeft, RotateRight
        }

        public Boolean active = true;

        PlayerState playerState;

        public PlayerState PlayerStates
        {
            get { return this.playerState; }
            set { this.playerState = value; }
        }

        public Player(Vector2 position, Vector2 size)
            : base(position, MagnetType.Polar, 100.0f, 0.0f, (float)Math.PI/2, false)
        {
            this.texture = TextureManager.LoadTexture("magnetMan");
            this.size = new Vector2(64.0f, 96.0f);
            this.origin = new Vector2(this.size.X / 2, this.size.Y / 2);
            this.playerState = PlayerState.Normal;
        }

        public void WallCollision()
        {
            float maxX = 912 - this.size.X;
            float maxY = 725 - this.size.Y;

            if (this.position.X > maxX)
            {
                this.velocity = Vector2.Reflect(this.velocity, new Vector2(1, 0));
            }
            else if (this.position.X < 112)
            {
                this.velocity = Vector2.Reflect(this.velocity, new Vector2(-1, 0));
            }

            if (this.position.Y > maxY)
            {
                this.velocity = Vector2.Reflect(this.velocity, new Vector2(0, 1));
            }
            else if (this.position.Y < 125)
            {
                this.velocity = Vector2.Reflect(this.velocity, new Vector2(0, -1));
            }
        }

        public override void Update(float delta)
        {
            if (active)
            {
                float d = (delta / World.NORMAL_TIME_CHANGE) * 0.001f;
                position.X += velocity.X * d;
                position.Y += velocity.Y * d;
            }
            WallCollision();
            base.Update(delta);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }

        public void StartAnimation(int FPS, PlayerState playerState, bool loop)
        {
            this.FPS = (float)1 / FPS;
            this.loop = loop;
            this.playerState = playerState;
            setAnimationSequence();
        }

        public void setAnimationSequence()
        {
            switch (this.playerState)
            {
                case PlayerState.Dying:
                    this.isAnimating = true;
                    this.resetFrame = 2;
                    this.startFrame = 2;
                    this.stopFrame = 4;
                    this.frameCount = 2;
                    break;
                case PlayerState.Moving:
                    this.isAnimating = true;
                    this.resetFrame = 0;
                    this.startFrame = 0;
                    this.stopFrame = 2;
                    this.frameCount = 2;
                    break;
                case PlayerState.Normal:
                    this.isAnimating = false;
                    this.resetFrame = 4;
                    this.startFrame = 4;
                    this.stopFrame = 4;
                    this.frameCount = 1;
                    break;
                case PlayerState.SlowMotion:
                    break;
                case PlayerState.RotateLeft:
                    this.isAnimating = false;
                    this.resetFrame = 0;
                    this.startFrame = 0;
                    this.stopFrame = 0;
                    this.frameCount = 1;
                    break;
                case PlayerState.RotateRight:
                    this.isAnimating = false;
                    this.resetFrame = 1;
                    this.startFrame = 1;
                    this.stopFrame = 1;
                    this.frameCount = 1;
                    break;
                case PlayerState.Spawning:
                    this.stopFrame = 4;
                    break;

            }
        }
    }
}


